enum QOS{
    //%block="qos0"
    qos0,
    //%block="qos1"
    qos1,
    //%block="qos2"
    qos2
}
enum UART{
    //%block="Hard serial port 1"
    Serial1,
    //%block="Hard serial port 2"
    Serial2,
    //%block="Soft serial port"
    SoftSerial
}
//% color="#33CCFF" iconWidth=50 iconHeight=40
namespace ML307R{

      //% block="Initialize ML 307 R module [UART] RX [RX] TX [TX] baud rate 115200 until successful" blockType="command"
      //% RX.shadow="dropdown" TX.shadow="dropdown" TX.options="PIN_TX" RX.options="PIN_RX"
      //% UART.shadow="dropdown" UART.options="UART" UART.defl="UART.Serial1"
      export function begin(parameter: any, block: any) {
        let rx = parameter.RX.code
        let tx = parameter.TX.code
        let uart = parameter.UART.code
        Generator.addInclude('XYXKC_ML307RInclude', '#include <XYXKC_ML307R.h>');
          Generator.addObject('XYXKC_ML307Rbject', 'XYXKC_ML307R',`XYXKC_ML307R;`);
          if (uart == "SoftSerial"){
              Generator.addInclude('SoftwareSerialInclude', '#include <SoftwareSerial.h>');
              Generator.addObject('SoftwareSerialbject', 'SoftwareSerial',`SoftSerial(${rx}, ${tx});`);
              Generator.addSetup(`XYXKC_ML307RSetup`,`XYXKC_ML307R.begin(&SoftSerial);`)
          }else{
              Generator.addSetup(`XYXKC_ML307RSetup`,`XYXKC_ML307R.begin(&${uart},${rx}, ${tx});`)
          }
      }
    //% block="ML307R module initiates MQTT connection account [USER] password [PASS]" blockType="command"
    //% USER.shadow="String" USER.defl="xyxedu2024001"
    //% PASS.shadow="String" PASS.defl="123456"
    export function mqttBegin(parameter: any, block: any) {
        let user = parameter.USER.code
        let pass = parameter.PASS.code
        Generator.addCode(`XYXKC_ML307R.mqttBegin(${user}, ${pass});`)
    }
    //% block="ML 307 R module subscription MQTT topic [TOPIC]" blockType="command"
    //% TOPIC.shadow="String" TOPIC.defl="xyxedu/2024001"
    export function mqttSubscribe(parameter: any, block: any) {
        let topic = parameter.TOPIC.code
        Generator.addCode(`XYXKC_ML307R.mqttSubscribe(${topic});`)
    }

    //% block="ML 307 R module sends MQTT message [MSG] to [TOPIC] topic" blockType="command"
    //% MSG.shadow="String" MSG.defl="hello"
    //% TOPIC.shadow="String" TOPIC.defl="xyxedu/2024001"
    export function mqttPublish(parameter: any, block: any) {
        let msg = parameter.MSG.code
        let topic = parameter.TOPIC.code
        Generator.addCode(`XYXKC_ML307R.mqttPublish(${topic},${msg});`)
    }


    //% block="MQTT connection successful?" blockType="boolean"
    export function isMqttConnectionSuccessful(parameter: any, block: any) {
        Generator.addCode(`(XYXKC_ML307R.isMqttConnectionSuccessful())`)
    }
    let contu = 0
    //% block="When receiving a message from the [TOPIC] topic" blockType="hat"
    //% TOPIC.shadow="String" TOPIC.defl="xyxedu/2024001"

    export function setCustomMqttCallbackNumber(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        let funcName = `whentReceived`
        let id = `${contu}`
        contu++
        Generator.addSetup(`${funcName}_${id}`, `XYXKC_ML307R.setMqttMsgCallback(${topic},${funcName}_${id});`);
        Generator.addEvent(`${funcName}_${id}`, "void", `${funcName}_${id}`, "String message", true);
    }


    //% block="mqtt message" blockType="reporter"
    export function getparsingData(parameter: any, block: any) {
        Generator.addCode(`message`)
    }
}